package com.qk.management.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.role.RoleDTO;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Override
    public PageResult<Role> page(RoleDTO roleDTO) {
        PageHelper.startPage(roleDTO.getPage(),roleDTO.getPageSize());
        List<Role> roles = roleMapper.getRoles(roleDTO);
        Page<Role> roleInfo = (Page<Role>) roles;
        return PageResult.<Role>builder()
                .total(Long.valueOf(roleInfo.getTotal()).intValue())
                .rows(roles)
                .build();
    }

    @Override
    public void delete(Integer id) {
        roleMapper.delete(id);
    }

    @Override
    public void save(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }

    @Override
    public Role get(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public void update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
    }

    @Override
    public List<Role> list() {
        return roleMapper.selectList();
    }
}
